package com.example.match.mui;

import android.app.ActionBar;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.CountDownTimer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import android.widget.Toast;

import com.example.match.R;
import com.example.mylibrary.util.Logger;
import com.example.mylibrary.util.SysApplication;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.aratek.facelib.AraErrorCode;
import cn.com.aratek.facelib.AraFaceAuthLib;
import cn.com.aratek.facelib.FaceAuthListeners;



public class MainUIActivity extends AppCompatActivity {
    private static final int SHOW_ARASERIVICE_DIALOG = 0;
    private static final int DISMISS_ARASERIVICE_DIALOG = 1;
    private ExecutorService executorService;
    private static AraFaceAuthLib araFaceAuthLib;
    private ProgressDialog mProgressDialog;
    public Logger logger;
    private boolean isOpenWifi = false;
    private WifiManager wm;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_ui);
        ButterKnife.bind(this);
        SysApplication.getInstance().addActivity(this);
        ActionBar actionBar =  getActionBar();
        if(actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        initData();
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:   //返回键的id
                finish();
                break;

        }
        return true;

    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //关闭整个程序
        SysApplication.getInstance().exit();


    }

    private void initData() {
        //亚略特是否授权
        wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        executorService = Executors.newCachedThreadPool();
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setIcon(android.R.drawable.ic_dialog_info);
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(false);
        araFaceAuthLib = new AraFaceAuthLib(this);
        //     showProgressDialog();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                timer.start();
                araFaceAuthLib.setStatusListener(listener);
                araFaceAuthLib.bind();
            }
        });

    }
    public static AraFaceAuthLib getAraFaceAuthLib() {
        return araFaceAuthLib;

    }

    private CountDownTimer timer = new CountDownTimer(10000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            timer.cancel();
        }
    };

    private FaceAuthListeners.StatusListener listener = new FaceAuthListeners.StatusListener() {

        @Override
        public void onServiceDisConnected() {
            timer.onFinish();
        }

        @Override
        public void onServiceConnected() {
            araFaceAuthLib.setDebug(true);
            mProgressDialog.setMessage("正在初始化...");

            if (!wm.isWifiEnabled()) {
                isOpenWifi = false;
                toggleWiFi(MainUIActivity.this, true);
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                isOpenWifi = true;
            }
            int a = araFaceAuthLib.initFaceEngine(270);
            if (a != AraErrorCode.ARA_OK) {
                a = araFaceAuthLib.initFaceEngine(270);
            }
            if (!isOpenWifi) {
                toggleWiFi(MainUIActivity.this, false);
            }
            String msg = (a == AraErrorCode.ARA_OK) ? "核心初始化成功" : "核心初始化失败";
            Toast.makeText(MainUIActivity.this, msg, Toast.LENGTH_SHORT).show();
            timer.onFinish();
        }
    };

    private void toggleWiFi(Context context, boolean enabled) {
        wm.setWifiEnabled(enabled);
    }

    @Override
    protected void onDestroy() {
        araFaceAuthLib.cleanListeners();
        araFaceAuthLib.releaseFaceEngine();
        super.onDestroy();
    }



    @OnClick({R.id.ry_face_check,R.id.ry_info_manage})
    public void onClick(View view){
       switch (view.getId()){
           case R.id.ry_face_check:
               Intent intent = new Intent(this, BlankPageActivity.class);
               startActivity(intent);
               break;

           case R.id.ry_info_manage:
               Intent intent1 = new Intent(this, InfoManageActivity.class);
               startActivity(intent1);
               break;
       }
   }
 }
